package com.ccn.crm.util;



import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

public class HttpUtil {
	
	/** 
     * @param url 接口地址
     * @param json  参数
     * @param method http调用方法
     * @param contentType 传递的参数格式
     * @return 
     */  
    @SuppressWarnings("unused")
	public static String httpProxy(String url,String json,String method,String contentType){
        InputStream is = null;  //输入流
        OutputStream os = null;  //输出流
        boolean is_success = true; // 接口调用成功标识
        try {  
            if(method==null){ //默认http请求为GET请求
                method = "GET";  
            }  
            if(contentType==null){ //默认字符编码为UTF-8 
                contentType = "utf-8";  
            }  
            URL _url = new URL(url); //定义一个url对象 
            HttpURLConnection conn = (HttpURLConnection) _url.openConnection();  //打开url连接
            conn.setDoInput(true);  //设置conn输出动作为true   
            conn.setDoOutput(true); //设置conn输入动作为true 
            //设置请求头信息
            conn.setRequestProperty("Content-type", contentType);  //设置参数格式
            if(!"DELETE".endsWith(method)){
            	if(json!=null){//接收参数  
                    os = conn.getOutputStream();//得到http连接输出  
                    os.write(json.getBytes("UTF-8"));  //往http连接里面传参
                    os.flush();  
                }  
            }

            if(conn.getResponseCode() == 200){
            	is = conn.getInputStream();  //获取返回值  
            }else{
            	is = conn.getErrorStream();
            }
            return getContent(is, "utf-8");//解析请求返回值
        } catch (MalformedURLException e) {  
            is_success = false;  //如果出错就直接返回失败标识
            e.printStackTrace();  
        } catch (IOException e) {  
            is_success = false;  //如果出错就直接返回失败标识
            e.printStackTrace();  
        } finally{  
            try {  
                if(os!=null){os.close();}  //如果输出流不为空就关闭出流
                if(is!=null){is.close();}   //如果输入流不为空就关闭输入流
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return null;  
    }


    public static String getContent(InputStream is, String charset) {
        String pageString = null;  //定义一个返回字符
        InputStreamReader isr = null;  //定义一个输入流读取
        BufferedReader br = null;  //定义一个缓存读取对象
        StringBuffer sb = null;
        try {
            isr = new InputStreamReader(is, charset);
            br = new BufferedReader(isr);
            sb = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) { //读取 输入流并赋值到sb中
                sb.append(line + "\n");
            }
            pageString = sb.toString(); //把 sb转变成String并赋值给pageString用于返回
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null){
                    is.close(); //关闭流对象
                }
                if(isr!=null){
                    isr.close(); //关闭流对象
                }
                if(br!=null){
                    br.close();  //关闭流对象
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            sb = null;
        }
        return pageString;  //返回结果
    }

}
